﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RegistrationController;

namespace HPMDesktop.Patient
{
    public partial class frmAddUpdateProcedure : Form
    {
        private Form _objParentForm;
        private string _parentClass;
        private string _patientName;
        private ProcedureDetails _objProcedure;
        private bool _addProcedure;
        private Hashtable _procedureInfo;
        private Hashtable _consultantInfo;

        public frmAddUpdateProcedure(bool addProcedure,
                                     Form objParentForm, 
                                     string parentClass,
                                     string patientName,
                                     ProcedureDetails objProcedure)
        {
            InitializeComponent();
            _addProcedure = addProcedure;
            _objParentForm = objParentForm;
            _parentClass = parentClass;
            _patientName = patientName;
            _objProcedure = objProcedure;
        }

        private void frmAddUpdateProcedure_Load(object sender, EventArgs e)
        {
            txtpname.Text = _patientName;
            txtCRNum.Text = _objProcedure.CRNumber;

            if (_addProcedure)
            {
                /* Load procedures from db */
                Procedure_DataLoader objProcDataLoader = new Procedure_DataLoader();

                _procedureInfo = objProcDataLoader.Get_Procedure_Data();

                foreach (String procName in _procedureInfo.Keys)
                {
                    cmbProcedures.Items.Add(procName);
                }

                objProcDataLoader.Dispose();

                /* Load consultants from db */
                ConsultantInfo_DataLoader objCIDataLoader = new ConsultantInfo_DataLoader();

                _consultantInfo = objCIDataLoader.Get_ConsultantFees_Data();

                foreach (String consultantName in _consultantInfo.Keys)
                {
                    cmbConsultant.Items.Add(consultantName);
                }

                cmbConsultant.SelectedText = _objProcedure.Consultant;

                objCIDataLoader.Dispose();
            }
            else
            {
                cmbProcedures.Enabled = false;
                cmbConsultant.Enabled = false;
                chkPaid.Enabled = false;
                lblAmount.Visible = false;
                txtProcAmount.Visible = false;

                cmbProcedures.Items.Add(_objProcedure.ProcedureName);
                cmbProcedures.Text = _objProcedure.ProcedureName;

                cmbConsultant.Items.Add(_objProcedure.Consultant);
                cmbConsultant.Text = _objProcedure.Consultant;

                chkPaid.Checked = _objProcedure.Paid;
            }
        }

        private void cmbProcedures_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_addProcedure &&
                cmbProcedures.Text != "" &&
                _procedureInfo != null)
            {
                string procName = cmbProcedures.Text;
                txtProcAmount.Text = _procedureInfo[procName].ToString();
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (cmbProcedures.Text == "")
            {
                MessageBox.Show("Please select a Procedure.");
            }
            else
            {
                _objProcedure.Paid = chkPaid.Checked;
                _objProcedure.ProcedureName = cmbProcedures.Text;
                _objProcedure.Consultant = cmbConsultant.Text;
                _objProcedure.ReceiptID = txtReceiptID.Text;
                _objProcedure.Remarks = txtRemarks.Text;

                Procedure_Mapper objProcMapper = new Procedure_Mapper();
                if (objProcMapper.set_procedure(_objProcedure))
                {
                    if (_parentClass.Equals("frmPatientDetails"))
                    {
                        ((frmPatientDetails)_objParentForm).updateProcedureView();
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Error in adding procedure. Try again later.");
                }
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
